package com.systemic.service.impl;

import com.systemic.mapper.MindfulnessClassMapper;
import com.systemic.pojo.MindfulnessClass;
import com.systemic.service.MindfulnessClassService;
import com.systemic.utils.FileUtils;
import com.systemic.vo.PageVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.net.URL;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: MindfulnessClassServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-26 10:02
 * @Version:
 **/
@Service
@PropertySource("classpath:/properties/mindfulness.properties")
public class MindfulnessClassServiceImpl implements MindfulnessClassService {

    // 正念类别的图片
    @Value("${mindfulness.mindfulnessClassURL}")
    private String mindfulnessClassURL;
    @Value("${mindfulness.mindfulnessClass}")
    private String mindfulnessClassImagePath;


    @Resource
    private MindfulnessClassMapper mindfulnessClassMapper;

    /**
     * 新增
     *
     *
     * @param image
     * @param mindfulnessClass
     * @return
     */
    @Override
    @Transactional
    public int add(MultipartFile image, MindfulnessClass mindfulnessClass) {
        int row = 0;
        if (mindfulnessClass!=null){
            Date date = new Date();
            mindfulnessClass.setGmtCreate(date);
            mindfulnessClass.setGmtModified(date);
            String url = FileUtils.uploadImage(image, mindfulnessClassImagePath, mindfulnessClassURL);
            mindfulnessClass.setImgUrl(url);
            row = mindfulnessClassMapper.insert(mindfulnessClass);
        }
        if (row==0){
            throw new RuntimeException("新增失败");
        }
        return row;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int delete(Integer id) {
        int row = 0;
        if (id!=0){
            // TODO: 删除正念关系、相关文件
            MindfulnessClass res = mindfulnessClassMapper.selectById(id);
            if (res!=null){
                // url转换对应的本地路径
                try {
                    URL url = new URL(res.getImgUrl());
                    String fileName = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
                    if (FileUtils.delete(mindfulnessClassImagePath + fileName)){
                        // 删除关联的数据记录
                        row = mindfulnessClassMapper.delete(res);
                    }else{
                        System.out.println("文件不存在");
                    }
                } catch (Exception e){
                    throw new RuntimeException("错误"+e);
                }
            }
        }
        return row;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public int deleteList(Integer[] ids) {
        int row = 0;
        if (ids.length!=0){
            List<Integer> idList = Arrays.asList(ids);
            // TODO: 删除正念关系、相关文件
            for (Integer id:ids) {
                MindfulnessClass res = mindfulnessClassMapper.selectById(id);
                if (res!=null){
                    // url转换对应的本地路径
                    try {
                        URL url = new URL(res.getImgUrl());
                        String fileName = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
                        if (FileUtils.delete(mindfulnessClassImagePath + fileName)){
                            // 删除关联的数据记录
                            row = mindfulnessClassMapper.delete(res) + row;
                        }else{
                            System.out.println("文件不存在");
                        }
                    } catch (Exception e){
                        throw new RuntimeException("错误"+e);
                    }
                }
            }
            row = mindfulnessClassMapper.deleteBatchIds(idList) + row;
        }
        return row;
    }

    /**
     * 修改
     *
     *
     * @param image
     * @param mindfulnessClass
     * @return
     */
    @Override
    @Transactional
    public int update(MultipartFile image, MindfulnessClass mindfulnessClass) {
        int row = 0;
        if (mindfulnessClass!=null){
            MindfulnessClass res = mindfulnessClassMapper.selectById(mindfulnessClass.getId());
            if (res!=null){
                if (!image.isEmpty()){
                    try {
                        URL url = new URL(res.getImgUrl());
                        String fileName = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
                        if (FileUtils.delete(mindfulnessClassImagePath + fileName)){
                            String new_url = FileUtils.uploadImage(image, mindfulnessClassImagePath, mindfulnessClassURL);
                            mindfulnessClass.setImgUrl(new_url);
                            Date date = new Date();
                            mindfulnessClass.setGmtModified(date);
                            row = mindfulnessClassMapper.update(mindfulnessClass) + row;
                        }
                    } catch (Exception e){
                        throw new RuntimeException("错误"+e);
                    }
                }
            }
        }
        if (row==0){
            throw new RuntimeException("修改失败");
        }
        return row;
    }

    /**
     * 指定查询
     *
     * @param id
     * @return
     */
    @Override
    public MindfulnessClass selectById(Integer id) {
        MindfulnessClass mindfulnessClass = new MindfulnessClass();
        if (id!=0){
            mindfulnessClass = mindfulnessClassMapper.selectById(id);
        }
        return mindfulnessClass;
    }

    /**
     * 根据名称模糊查询
     *
     * @param className
     * @return
     */
    @Override
    public List<MindfulnessClass> likeSelect(String className) {
        MindfulnessClass mindfulnessClass = new MindfulnessClass();
        mindfulnessClass.setClassName(className);
        return mindfulnessClassMapper.likeSelect(mindfulnessClass);
    }

    /**
     * 模糊分页查询
     *
     * @param pageVo
     * @return
     */
    @Override
    public List<MindfulnessClass> likeSelectPage(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        String className = pageVo.getClassName();
        return mindfulnessClassMapper.likeSelectPage(className,offset,pageSize);
    }
}
